home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / com / internet / stik / gluestik / gsdaemon.c < prev    next >
C/C++ Source or Header  |  1996-09-01  |  3KB  |  162 lines

  1. #include <osbind.h>
  2. #include <mintbind.h>
  3. #include <string.h>
  4. #include <setjmp.h>
  5. #include <unistd.h>
  6. #include <support.h>
  7. #include "patchlev.h"
  8. #include "global.h"
  9. #include "pipe.h"
  10.  
  11.  
  12. long _stksize = 65536L;
  13.  
  14. /* all the memory that cleanup() might have to deallocate */
  15. extern unsigned char *file_data;
  16. extern Var *vars;
  17. extern unsigned char *pool;
  18.  
  19. #if 0
  20. static Daemon_Interface DMN_ = {GSDAEMON_MAGIC, &OP, 0, 0, 0};
  21. Daemon_Interface *DMN = &DMN_;
  22. #endif
  23.  
  24. extern void dispatch __PROTO((void));
  25.  
  26. static jmp_buf  tforkj;
  27. static int in_tfork(arg)
  28. int arg;
  29. {
  30.   /* wait for parent to die before we can longjmp back */
  31.   while (getppid () > 1)
  32.     sleep (1);
  33.   longjmp (tforkj, 1);
  34.   /*NOTREACHED*/
  35.   return 0;
  36. }
  37.  
  38. void hexify(register unsigned long l, register char *buf)
  39. {
  40.   register char *s = buf + 8;
  41.   static char digits[] = "0123456789ABCDEF";
  42.  
  43.   *s = 0;
  44.   while (s > buf) {
  45.     *--s = digits[l & 0xF];
  46.     l >>= 4;
  47.   }
  48. }
  49.  
  50. /* cleanup() -- free everything we might have alloced, and destroy all
  51.    semaphores we might have created. */
  52. static void cleanup(void)
  53. {
  54.   Mfree(file_data);
  55.   Mfree(vars);
  56.   Mfree(pool);
  57.   /* We have to be holding the semaphore to destroy it */
  58.   Psemaphore(2, DMN_SEMAPHORE, -1L);
  59.   Psemaphore(1, DMN_SEMAPHORE, 0L);
  60. #ifdef DEBUG
  61.   Psemaphore(2, DEBUG_SEMAPHORE, -1L);
  62.   Psemaphore(1, DEBUG_SEMAPHORE, 0L);
  63. #endif /* DEBUG */
  64. }
  65.  
  66. int main(void)
  67. {
  68.   char *s;
  69. #if 0
  70.   char dmn_arg[10];
  71.   long l;
  72. #endif
  73.  
  74.   Cconws("\r\nGlueSTiK\277 STiK emulator for MiNTnet\r\n"
  75.      "Network interface daemon\r\n"
  76.      "Version " GS_VERSION "\r\n"
  77.      "\275 1996 Scott Bigham\r\n");
  78. #ifdef DEBUG
  79.   Cconws("    Debug logging enabled\r\n");
  80. #endif
  81. #ifdef BLOCK_OPEN
  82.   Cconws("    Non-blocking open disabled\r\n");
  83. #endif
  84.  
  85. #ifdef DEBUG
  86.   debug("s", "Starting GSDaemon v" GS_VERSION);
  87. #endif
  88.  
  89.   if (load_config_file() < 0) {
  90.     cleanup();
  91.     return -1;
  92.   }
  93. #ifdef DEBUG
  94.   debug("s", "Config file loaded");
  95. #endif
  96.  
  97.   s = do_getvstr("CDVALID");
  98.  
  99. #ifdef DEBUG
  100.   debug("ss", "CDVALID = ", s);
  101. #endif
  102.   if (s && (!strcmp(s, "1") || !stricmp(s, "TRUE") || !stricmp(s, "ON")))
  103.     Cconws("WARNING:  CDVALID carrier-detect not supported\r\n");
  104.  
  105.   if (!init_mem()) {
  106.     cleanup();
  107.     return -1;
  108.   }
  109. #ifdef DEBUG
  110.   debug("s", "Alloc pool initialized");
  111. #endif
  112.  
  113.   if (!init_net()) {
  114.     cleanup();
  115.     return -1;
  116.   }
  117. #ifdef DEBUG
  118.   debug("s", "Network functions initialized");
  119. #endif
  120.  
  121. #if 0
  122.   dmn_arg[0] = 8;
  123.   hexify((unsigned long)DMN, dmn_arg + 1);
  124. #ifdef DEBUG
  125.   debug("ss", "Ifc address is ", dmn_arg + 1);
  126. #endif
  127.  
  128.   s = do_getvstr("GSDRIVER");
  129.   if (!s)
  130.     s = GSDRIVER_DEFAULT;
  131.   l = Pexec(0, s, dmn_arg, NULL);
  132. #ifdef DEBUG
  133.   debug("sl", "Driver returns ", l);
  134. #endif
  135.   if (l != 0) {
  136.     Cconws("Driver failed --- exiting\r\n");
  137.     return -1;
  138.   }
  139. #endif /* 0 */
  140.  
  141.   Cconws("\r\n");
  142.  
  143.   /* "put-self-in-background" trick lifted from syslogd */
  144. #if 0    /* when MiNT gets a non-blocking fork(), this will work */
  145.   if (fork())
  146.     exit(0);
  147. #else    /* until then, we use this */
  148.   if (!setjmp(tforkj) && tfork (in_tfork, 0) >= 0)
  149.     _exit (0);
  150. #endif
  151.  
  152. #if 0
  153.   DMN->daemon_pid = getpid();
  154. #ifdef DEBUG
  155.   debug("sd", "Daemon pid = ", DMN->daemon_pid);
  156. #endif
  157. #endif
  158.  
  159.   dispatch();
  160.   return 0;
  161. }
  162.